package com.orhon.smartcampus.modules.systemctl.mutation;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.VersionManagementDao;
import com.orhon.smartcampus.modules.systemctl.entity.VersionManagement;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLVersionManagementInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.VersionManagementInputMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AddVersionManagementMutation implements DataFetcher {



    @Autowired
    VersionManagementDao versionManagementDao;

    /**
     * 新增/修改版本管理的维护
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLVersionManagementInput input = VersionManagementInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        VersionManagement versionManagement = null;

        if (input.getId() != null) {
            versionManagement = versionManagementDao.get(VersionManagement.class, input.getId());
        } else {
            versionManagement = new VersionManagement();
            versionManagement.setCreateTime(new Date());
        }
        if(input.getTitle() != null){

            versionManagement.setTitle(input.getTitle());
        }
        if(input.getRemark() != null){

            versionManagement.setRemark(input.getRemark());
        }
        if(input.getType() != null) {
            versionManagement.setType(input.getType());
        }
        if(input.getVersionNumber() != null){

            versionManagement.setVersionNumber(input.getVersionNumber());
        }
        if(input.getVersionUrl() != null){

            versionManagement.setVersionUrl(input.getVersionUrl());
        }
        try{
            versionManagementDao.saveOrUpdate(versionManagement);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

